package com.istudy.dao.impl;

import com.istudy.dao.BaseDao;
import com.istudy.dao.interfaces.VideoDao;
import com.istudy.entity.Video;
import com.istudy.utils.Page;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author GQ
 * 视频数据实现类
 */
public class VideoDaoImpl implements VideoDao {
    private BaseDao baseDao = new BaseDao();
    /**
     * 获取显示列表
     *
     * @return 实体类 集合
     * @throws Exception 可能出现的错误
     */

    @Override
    public List getshowlist(Page page ,int type) throws Exception {
        List<Video> list = new ArrayList<Video>();
        Video video =null;
        String sql = "select * from video where `video_type`=? LIMIT ?,8";
        List<Object> objects=new ArrayList<>();
        objects.add(type);
        objects.add(page.getPageSize()*(page.getPageNo()-1));
        ResultSet rs = baseDao.executeQuery(sql, objects.toArray());
        while (rs.next()) {
           video = new Video();
            fill(video, rs);
            list.add(video);
        }
        return list;
    }

    /**
     * 获取显示列表
     *
     * @param name
     * @return 实体类 集合
     * @throws Exception 可能出现的错误
     */
    @Override
    public List<Video> getVideoListByName(String name ,Page page) {
        List<Video> list = new ArrayList<Video>();
        Video video =null;
        StringBuffer sql =new StringBuffer();
       sql.append( "select * from video where `video_name` like  concat ('%',");
        sql.append("?");
        sql.append(",'%')");
        sql.append("LIMIT ?,8");
        List<Object> objects=new ArrayList<>();
        objects.add(name);
        objects.add(page.getPageSize()*(page.getPageNo()-1));
        ResultSet rs = baseDao.executeQuery(sql.toString(), objects.toArray());
        try{
        while (rs.next()) {
            video = new Video();
            fill(video, rs);
            list.add(video);
        }
        }catch (Exception e){

        }
        return list;
    }

    /**
     * 获取总记录数
     *
     * @return 记录数
     */
    @Override
    public int getCount(int type) {

        String sql ="SELECT COUNT(1) as count FROM video where `video_type`= "+type;
        ResultSet rs = baseDao.executeQuery(sql);
        try {
            if (rs.next()){
                return rs.getInt("count");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }
    @Override
    public int getCount(String name) {
        StringBuffer sql =new StringBuffer();
        sql.append( "SELECT COUNT(1) as count from video where `video_name` like  concat ('%',");
        sql.append("?");
        sql.append(",'%')");
        List<Object> objects=new ArrayList<>();
        objects.add(name);
        ResultSet rs = baseDao.executeQuery(sql.toString(),objects.toArray());
        try {
            if (rs.next()){
                return rs.getInt("count");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }
    /**
     * 添加视频
     *
     * @param video 视频实体类
     * @return 受影响行数
     */
    @Override
    public int insertVideo(Video video) {
        String sql = "INSERT INTO `video`(`video_name`,`video_address`,`video_consume`,`video_type`,`video_picture`,`video_descrtion`,`video_upload_user`,`video_upload_time`)" +
                "VALUES (?,?,?,?,?,?,?,?)";
        return fillEntity(sql,video);
    }

    /**
     * 根据ID删除
     *
     * @param id id
     * @return 受影响行数
     */
    @Override
    public int deleteVideo(int id) {
        String sql = "DELETE FROM `video` WHERE `video_id` =?";
        Object[] object = new Object[]{id};

        return baseDao.executeUpdate(sql,object);
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return 实体类
     */
    @Override
    public Video querybyId(int id) {
        Video video = new Video();
        String sql = "select * FROM `video` WHERE `video_id` =?";
        Object[] object = new Object[]{id};
        ResultSet rs=  baseDao.executeQuery(sql,object);
        try {
            if (rs.next()) {
                fill(video, rs);
                return video;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    private void fill(Video video, ResultSet rs) throws SQLException {
        video.setVideoId( rs.getInt("video_id"));
        video.setVideoName( rs.getString("video_name"));
        video.setVideoAddress( rs.getString("video_address"));
        video.setVideoConsume( rs.getInt("video_consume"));
        video.setVideoType( rs.getString("video_type"));
        video.setVideoPicture( rs.getString("video_picture"));
        video.setVideoDescrtion( rs.getString("video_descrtion"));
        video.setVideoUploadUser( rs.getString("video_upload_user"));
        video.setVideoUploadTime( rs.getString("video_upload_time"));
    }

    /**
     * 修改视频
     *
     * @param  video 视频类
     * @return 受影响行数
     */
    @Override
    public int updataVideo(Video video) {
        String sql = "UPDATE `video` SET `video_name`=?,`video_address`=?,`video_consume`=?,`video_type`=?,`video_picture`=?, video_length=?,video_descrtion=?,video_upload_user=?,video_upload_time=? WHERE `video_id` =?" ;

        return fillEntity(sql,video);
    }

    private int fillEntity(String sql,Video video) {

        Object[] object = new Object[]{
                video.getVideoName(),
                video.getVideoAddress(),
                video.getVideoConsume(),
                video.getVideoType(),
                video.getVideoPicture(),
                video.getVideoDescrtion(),
                video.getVideoUploadUser(),
                video.getVideoUploadTime(),
        };
        int i =  baseDao.executeUpdate(sql,object);
        return i;
    }
}
